package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.constant.PasswordConstant;
import com.campus.constant.RoleConstant;
import com.campus.dao.ManagerStudentMapper;
import com.campus.entry.ManagerStudent;
import com.campus.pojo.dto.RegisterDto;
import com.campus.service.AccountService;
import com.campus.service.ManagerStudentService;
import com.campus.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerStudentServiceImpl implements ManagerStudentService {
    @Autowired
    private ManagerStudentMapper managerStudentMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AccountService accountService;


    /**
     * 新增学员
     * @param managerStudent
     */
    @Override
    public void addStudent(ManagerStudent managerStudent) {
        managerStudent.setStatus(0);
        String loginName=managerStudent.getParentPhone();
        RegisterDto registerDto=new RegisterDto(loginName, PasswordConstant.DEFAULT_PASSWORD, RoleConstant.STUDENT);
        accountService.register(registerDto);
        Integer userIdByUserName = sysUserService.getUserIdByUserName(loginName);
        managerStudent.setLoginId(userIdByUserName);
        managerStudentMapper.insert(managerStudent);

    }

    /**
     * 批量导入学员
     * @param managerStudentList
     */
    @Override
    public void addStudentList(List<ManagerStudent> managerStudentList) {
        for (ManagerStudent managerStudent : managerStudentList) {
            addStudent(managerStudent);
        }
    }

    @Override
    public List<ManagerStudent> lisStudents() {
        LambdaQueryWrapper<ManagerStudent> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        return managerStudentMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void updateSatus(Integer id) {
        managerStudentMapper.updateStatus(id);
    }
    /**
     * 根据筛选条件查询学员列表
     * @param stuName 姓名
     * @param grade 年级
     * @param isFirst 是否新生
     * @param status 审核状态
     * @return List<ManagerStudent>
     */
    @Override
    public List<ManagerStudent> listStudentsWithFilter(String stuName, String grade, String isFirst, Integer status) {
        LambdaQueryWrapper<ManagerStudent> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 添加筛选条件
        if (stuName != null && !stuName.isEmpty()) {
            lambdaQueryWrapper.like(ManagerStudent::getStuName, stuName);
        }
        if (grade != null && !grade.isEmpty()) {
            lambdaQueryWrapper.eq(ManagerStudent::getCurrentGrade, grade);
        }
        if (isFirst != null && !isFirst.isEmpty()) {
            lambdaQueryWrapper.eq(ManagerStudent::getIsFirst, isFirst);
        }
        if (status != null) {
            lambdaQueryWrapper.eq(ManagerStudent::getStatus, status);
        }

        return managerStudentMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public ManagerStudent getStudentById(Integer id) {
        System.out.println(id);
        ManagerStudent managerStudent = managerStudentMapper.selectById(id);
        return managerStudent;
    }

    @Override
    public Long countStudent() {
        LambdaQueryWrapper<ManagerStudent> studentLambdaQueryWrapper=new LambdaQueryWrapper<>();
        return managerStudentMapper.selectCount(studentLambdaQueryWrapper);

    }

    @Override
    public void updateStudent(ManagerStudent managerStudent) {
        LambdaQueryWrapper<ManagerStudent> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerStudent::getId,managerStudent.getId());
        managerStudentMapper.update(managerStudent,lambdaQueryWrapper);
    }

    @Override
    public void removeStudentById(Integer id) {
        LambdaQueryWrapper<ManagerStudent> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ManagerStudent::getId,id);
        managerStudentMapper.deleteById(id);
    }

    @Override
    public List<ManagerStudent> getStudent() {
        return managerStudentMapper.getMangerStudent();
    }

    @Override
    public Integer getStudentIdByLoinId(Integer loginId) {
        return managerStudentMapper.getStudentIdByLoginId(loginId);
    }

}
